package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增分类
     * @param category
     * @param session
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category,HttpSession session){
    /*    log.info("category:{}",category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Employee user = (Employee) session.getAttribute("loginUser");
        category.setCreateUser(user.getId());
        category.setUpdateUser(user.getId());*/
        boolean save = categoryService.save(category);
        if (save){
            return R.success("新增分类成功");
        }else {
           return R.error("保存失败");
        }
        /*categoryService.save(category);
        return R.success("新增分类成功");*/
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
    // 分页构造器
        Page pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> qw=new LambdaQueryWrapper<>();
        //添加排序条件,根据sort进行排序
        qw.orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(pageInfo,qw);

        return R.success(pageInfo);
    }

    /**
     * 分类删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(Long id){

        //1、检查当前分类下是否有对应的菜品
// SELECT COUNT(*) FROM `dish` WHERE category_id = 1397844263642378242
// 结果大于0 ，则不能删除  代码结束 --> 自定义一个业务异常，然后抛出  throw new BusinessException("该分类下有菜品，不能删除");
        LambdaQueryWrapper<Dish> qw =new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,id);
        int count = dishService.count(qw);
        //2、检查当前分类下是否有对应的套餐
// SELECT COUNT(*) FROM `setmeal` WHERE category_id= 1397844263642378242
//结果大于0 ，则不能删除  代码结束 --> 自定义一个业务异常，然后抛出 throw new BusinessException("该分类下有套餐，不能删除");
        LambdaQueryWrapper<Setmeal> wq =new LambdaQueryWrapper<>();
        wq.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(wq);
        if (count>0||count1>0){
            return R.error("分类下还有商品无法删除");
        }
        categoryService.removeById(id);
        return R.success(null);
        //3、可以删除,则调用mapper直接删除 categoryMapper.deleteById(id);
    }

    /**
     * 根据ID修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    /**
     * 根据条件查询分类信息
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R list(Integer type){
        //调用业务层,查询数据 select*from category where type = ?
       //创建条件过滤器
        LambdaQueryWrapper<Category> qw =new LambdaQueryWrapper<>();
        //执行过滤条件
       qw.eq(type!=null,Category::getType,type);
       //添加排序条件
        qw.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(qw);
        return R.success(list);

    }

}
